package com.heima.behavior.service.impl;

import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.behavior.service.ApReadBehaviorService;
import com.heima.common.exception.CustException;
import com.heima.model.behavior.dtos.ReadBehaviorDTO;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApLikesBehavior;
import com.heima.model.behavior.pojos.ApReadBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.common.threadlocal.AppThreadLocalUtils;
import com.heima.model.user.pojos.ApUser;
import com.mongodb.client.result.UpdateResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

@Service
@Slf4j
public class ApReadBehaviorServiceImpl implements ApReadBehaviorService {
    @Autowired
    ApBehaviorEntryService apBehaviorEntryService;
    @Autowired
    MongoTemplate mongoTemplate;

    @Override
    public ResponseResult readBehavior(ReadBehaviorDTO dto) {
        if (StringUtils.isEmpty(dto.getArticleId())) {
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID, "文章id为空");
        }
        ApUser user = AppThreadLocalUtils.getUser();
        if (user == null) {
            if (StringUtils.isEmpty(dto.getEquipmentId())) {
                CustException.cust(AppHttpCodeEnum.PARAM_INVALID, "不可操作");
            }
            ApBehaviorEntry apBehaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(user.getId(), dto.getEquipmentId());
            Query query = Query.query(Criteria.where("articleId").is(dto.getArticleId()).and("entryId").is(apBehaviorEntry.getId()));
            ApReadBehavior apReadBehavior = mongoTemplate.findOne(query, ApReadBehavior.class);
            log.info("查询阅读行为是否存在" + apReadBehavior);
            if (apReadBehavior == null) {//不存在
                ApReadBehavior apLikesBehavior2 = ApReadBehavior.builder()
                        .entryId(apBehaviorEntry.getId())
                        .count((short)1)
                        .articleId(dto.getArticleId())
                        .build();
                ApReadBehavior saveResult = mongoTemplate.insert(apLikesBehavior2);
                return ResponseResult.okResult(saveResult);
            }
            Update update = new Update();
            short i=(short) (apReadBehavior.getCount()+1);
            update.set("count", i).set("updatedTime", new Date());   // set修改器：只会对你指定列修改
            //        UpdateResult updateResult = mongoTemplate.updateFirst(query, update, ApComment.class);
            //        System.out.println(updateResult);
            UpdateResult updateResult = mongoTemplate.updateMulti(query, update, ApReadBehavior.class);
            System.out.println(updateResult);
            return ResponseResult.okResult(updateResult);

        }
        ApBehaviorEntry apBehaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(user.getId(), dto.getEquipmentId());
        Query query = Query.query(Criteria.where("articleId").is(dto.getArticleId()).and("entryId").is(apBehaviorEntry.getId()));
        ApReadBehavior apReadBehavior = mongoTemplate.findOne(query, ApReadBehavior.class);
        log.info("查询阅读行为是否存在" + apReadBehavior);
        if (apReadBehavior == null) {//不存在
            ApReadBehavior apLikesBehavior2 = ApReadBehavior.builder()
                    .entryId(apBehaviorEntry.getId())
                    .count((short)1)
                    .articleId(dto.getArticleId())
                    .build();
            ApReadBehavior saveResult = mongoTemplate.insert(apLikesBehavior2);
            return ResponseResult.okResult(saveResult);
        }
        Update update = new Update();
        short i=(short) (apReadBehavior.getCount()+1);
        update.set("count", i).set("updatedTime", new Date());   // set修改器：只会对你指定列修改
        //        UpdateResult updateResult = mongoTemplate.updateFirst(query, update, ApComment.class);
        //        System.out.println(updateResult);
        UpdateResult updateResult = mongoTemplate.updateMulti(query, update, ApReadBehavior.class);
        System.out.println(updateResult);
        return ResponseResult.okResult(updateResult);
    }
}
